package cn.deschen.common.api;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.List;

/**
 * @Author hanbin_chen
 * @Description 分页封装类
 * @DateTime 2021/6/1
 * @Version V1.0.0
 */
@Data
@EqualsAndHashCode(callSuper = false)
@ApiModel(value="创意广告查询参数", description="")
public class CmPage<T> {

    @ApiModelProperty(value = "当前页")
    private int currentPage;

    @ApiModelProperty(value = "当前页的数量")
    private int currentSize;

    @ApiModelProperty(value = "总页数")
    private int totalPageNum;

    @ApiModelProperty(value = "总记录数")
    private long totalRecordNum;

    @ApiModelProperty(value = "结果集")
    private List<T> recordList;

    /**
     * 将PageHelper分页后的list转为分页信息
     * @param list com.github.pagehelper.Page类型
     * @return
     */
    public static <T> CmPage<T> restPage(List<T> list) {
        // PageInfo将list的内容（即分页内容）抽取封装
        PageInfo<T> pageInfo = new PageInfo<>(list);
        CmPage<T> cmPage = new CmPage<>();
        cmPage.setCurrentPage(pageInfo.getPageNum());
        cmPage.setCurrentSize(pageInfo.getSize());
        cmPage.setTotalPageNum(pageInfo.getPages());
        cmPage.setTotalRecordNum(pageInfo.getTotal());
        cmPage.setRecordList(pageInfo.getList());
        return cmPage;
    }

}
